﻿using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace Artech.Core.Configuration
{
    /// <summary>
    /// This class is used as the base class of all custom configuration section.
    /// </summary>
    public class ServiceLocatableConfigurationSection : SerializableConfigurationSection, ITypeRegistrationsProvider
    {
        private const string PropertyName4ServiceLocatorName = "serviceLocator";

        /// <summary>
        /// Gets or sets name of the service locator.
        /// </summary>
        /// <value>
        /// The name of service locator.
        /// </value>
        [ConfigurationProperty(PropertyName4ServiceLocatorName, IsRequired = false, DefaultValue = "")]
        public string ServiceLocatorName
        {
            get { return base[PropertyName4ServiceLocatorName] as string; }
            set { base[PropertyName4ServiceLocatorName] = value; }
        }

        public IServiceLocator ServiceLocator
        {
            get { return ServiceLocatorBuilder.Current.GetServiceLocator(this.ServiceLocatorName); }
        }

        /// <summary>
        /// Return the <see cref="TypeRegistration" /> objects needed to configure
        /// the container.
        /// </summary>
        /// <param name="configurationSource">The <see cref="IConfigurationSource" /> containing
        /// the configuration information.</param>
        /// <returns>
        /// The sequence of <see cref="TypeRegistration" /> objects.
        /// </returns>
        public virtual IEnumerable<TypeRegistration> GetRegistrations(IConfigurationSource configurationSource)
        {
            return Enumerable.Empty<TypeRegistration>();
        }

        /// <summary>
        /// Return the <see cref="TypeRegistration" /> objects needed to reconfigure
        /// the container after a configuration source has changed.
        /// </summary>
        /// <param name="configurationSource">The <see cref="IConfigurationSource" /> containing
        /// the configuration information.</param>
        /// <returns>
        /// The sequence of <see cref="TypeRegistration" /> objects.
        /// </returns>
        /// <remarks>
        /// If there are no reregistrations, return an empty sequence.
        /// </remarks>
        public virtual IEnumerable<TypeRegistration> GetUpdatedRegistrations(IConfigurationSource configurationSource)
        {
            return this.GetRegistrations(configurationSource);
        }
    }
}
